//
//  WithdrawViewController.m
//  cm
//
//  Created by suyibaishan on 2017/11/6.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "WithdrawViewController.h"

@interface WithdrawViewController () <UIScrollViewDelegate,UITextFieldDelegate>
{
    UIScrollView *_scrollView;
    NSDictionary *dataSource;
    UIView *headView;
    UITextField *priceField;
    UILabel *declareLabel;
    UIView *cardView;
    UILabel *nameLabel;
    UILabel *numberLabel;
    UILabel *bankLabel;
    UILabel *chargeLabel;
    UIView *whiteView;
    UIView *coverView;
    UIView *declareView;
    UILabel *coverLabel;
    UILabel *rebateLabel;
    UILabel *costLabel;
    UIView *bottomView;
    NSInteger type;
}
@end

@implementation WithdrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self reloadData];
    
    [self createWithdrawView];
}

-(void)reloadData
{
    [[ToolNet sharedManager] getUserInfo:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] url:MExchangePage completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            dataSource = responseJson[@"data"];
            nameLabel.text = dataSource[@"bankHolder"];
            numberLabel.text = dataSource[@"bankNumber"];
            bankLabel.text = dataSource[@"bankName"];
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }
    }];
}

-(void)createWithdrawView
{
    self.titleLabel.text = @"余额提现";
    self.backButton.hidden = NO;
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.baseBarHeight, kScreenWidth, kScreenHeight - self.baseBarHeight)];
    _scrollView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    UITapGestureRecognizer *scrollTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidekeyBoard)];
    [_scrollView addGestureRecognizer:scrollTap];
    
    headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    [_scrollView addSubview:headView];
    
    for (NSInteger i = 0; i < 2; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        button.frame = CGRectMake(i*kScreenWidth/2, 0, kScreenWidth/2, 50);
        [button setTitle:i == 0?@"现金提现":@"购车基金提现" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        button.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        button.tag = 100 + i;
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [headView addSubview:button];
        if (i == 0) {
            button.selected = YES;
            button.backgroundColor = [UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1];
            type = 0;
        }
    }
    
    whiteView = [[UIView alloc] init];
    whiteView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:whiteView];
    whiteView.sd_layout
    .leftSpaceToView(_scrollView, 0)
    .topSpaceToView(headView, 0)
    .widthIs(kScreenWidth)
    .heightIs(95);
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.text = @"提现金额";
    titleLabel.textColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    [whiteView addSubview:titleLabel];
    titleLabel.sd_layout
    .leftSpaceToView(whiteView, 20)
    .topSpaceToView(whiteView, 10)
    .widthIs(kScreenWidth - 80)
    .heightIs(20);
    
    UILabel *symbolLabel = [[UILabel alloc] init];
    symbolLabel.font = [UIFont systemFontOfSize:45];
    symbolLabel.text = @"¥";
    symbolLabel.textColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    [whiteView addSubview:symbolLabel];
    symbolLabel.sd_layout
    .leftSpaceToView(whiteView, 20)
    .topSpaceToView(titleLabel, 10)
    .widthIs(45)
    .heightIs(45);
    
    priceField = [[UITextField alloc] init];
    priceField.delegate = self;
    priceField.font = [UIFont systemFontOfSize:25];
    priceField.keyboardType = UIKeyboardTypeNumberPad;
    [priceField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [whiteView addSubview:priceField];
    priceField.sd_layout
    .leftSpaceToView(symbolLabel, 0)
    .topSpaceToView(titleLabel, 10)
    .widthIs(kScreenWidth/2)
    .heightIs(45);
    
    UIView *thinline = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    thinline.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [whiteView addSubview:thinline];
    thinline.sd_layout
    .leftSpaceToView(whiteView, 20)
    .bottomSpaceToView(whiteView, 0)
    .widthIs(kScreenWidth - 40)
    .heightIs(1);
    
    declareView = [[UIView alloc] init];
    declareView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:declareView];
    declareView.sd_layout
    .leftSpaceToView(_scrollView, 0)
    .topSpaceToView(whiteView, 0)
    .widthIs(kScreenWidth)
    .heightIs(110);
    
    declareLabel = [[UILabel alloc] init];
    declareLabel.font = [UIFont systemFontOfSize:12];
//    declareLabel.text = @"实际提现¥0.0,";
    declareLabel.textColor = [UIColor blackColor];
    [declareView addSubview:declareLabel];
    declareLabel.sd_layout
    .leftSpaceToView(declareView, 20)
    .topSpaceToView(declareView, 0)
    .widthIs(110)
    .heightIs(35);
    NSMutableAttributedString *declareStr = [[NSMutableAttributedString alloc] initWithString:@"实际提现¥0.0,"];
    [declareStr addAttribute:NSForegroundColorAttributeName
                    value:[UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1]
                    range:NSMakeRange(5, declareStr.length-6)];
    declareLabel.attributedText = declareStr;
    [declareLabel setSingleLineAutoResizeWithMaxWidth:kScreenWidth/2-20];
    
    chargeLabel = [[UILabel alloc] init];
    chargeLabel.font = [UIFont systemFontOfSize:12];
//    chargeLabel.text = @"扣除手续费500(手续费5%)";
    chargeLabel.textColor = [UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1];
    [declareView addSubview:chargeLabel];
    chargeLabel.sd_layout
    .leftSpaceToView(declareLabel, 0)
    .topSpaceToView(declareView, 0)
    .widthIs(120)
    .heightIs(35);
    NSMutableAttributedString *chargeStr = [[NSMutableAttributedString alloc] initWithString:@"扣除手续费¥0"];
    [chargeStr addAttribute:NSForegroundColorAttributeName
                       value:[UIColor blackColor]
                       range:NSMakeRange(0, 6)];
//    [chargeStr addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1],NSFontAttributeName:[UIFont systemFontOfSize:11]} range:NSMakeRange(chargeStr.length-7, 7)];
    chargeLabel.attributedText = chargeStr;
    [chargeLabel setSingleLineAutoResizeWithMaxWidth:kScreenWidth/2];
    
    UILabel *serviceLabel = [[UILabel alloc] init];
    serviceLabel.text = @"(手续费5%)";
    serviceLabel.font = [UIFont systemFontOfSize:11];
    serviceLabel.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
    [declareView addSubview:serviceLabel];
    serviceLabel.sd_layout
    .leftSpaceToView(declareView, 20)
    .topSpaceToView(chargeLabel, 0)
    .widthIs(150)
    .heightIs(20);

    UIView *bottomline = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    bottomline.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [declareView addSubview:bottomline];
    bottomline.sd_layout
    .leftSpaceToView(declareView, 0)
    .topSpaceToView(serviceLabel, 5)
    .widthIs(kScreenWidth)
    .heightIs(1);
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 17, 32, 21)];
    imageView.image = [UIImage imageNamed:@"cm_bankcard"];
    [declareView addSubview:imageView];
    imageView.sd_layout
    .leftSpaceToView(declareView, 15)
    .topSpaceToView(bottomline, 12)
    .widthIs(32)
    .heightIs(21);
    
    UILabel *cardLabel = [[UILabel alloc] init];
    cardLabel.text = @"提现到银行卡";
    cardLabel.font = [UIFont systemFontOfSize:15];
    cardLabel.textColor = [UIColor blackColor];
    [declareView addSubview:cardLabel];
    cardLabel.sd_layout
    .leftSpaceToView(imageView, 15)
    .topSpaceToView(bottomline, 5)
    .widthIs(120)
    .heightIs(35);
    
    [self createCoverView];
    
    cardView = [[UIView alloc] init];
    [_scrollView addSubview:cardView];
    cardView.sd_layout
    .leftSpaceToView(_scrollView, 0)
    .topSpaceToView(declareView, 0)
    .widthIs(kScreenWidth)
    .heightIs(135);
    
    NSArray *titleArr = @[@"开户人",@"卡号",@"开户行"];
    for (NSInteger i = 0; i < 3; i++) {
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.text = titleArr[i];
        titleLabel.font = [UIFont systemFontOfSize:14];
        [cardView addSubview:titleLabel];
        titleLabel.sd_layout
        .leftSpaceToView(cardView, 15)
        .topSpaceToView(cardView, 10+i*45)
        .widthIs(65)
        .heightIs(30);
    }
    
    nameLabel = [[UILabel alloc] init];
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.font = [UIFont systemFontOfSize:14];
    [cardView addSubview:nameLabel];
    nameLabel.sd_layout
    .leftSpaceToView(cardView, 80)
    .topSpaceToView(cardView, 10)
    .widthIs(kScreenWidth/2)
    .heightIs(30);
    
    numberLabel = [[UILabel alloc] init];
    numberLabel.textColor = [UIColor blackColor];
    numberLabel.font = [UIFont systemFontOfSize:14];
    [cardView addSubview:numberLabel];
    numberLabel.sd_layout
    .leftSpaceToView(cardView, 80)
    .topSpaceToView(nameLabel, 15)
    .widthIs(kScreenWidth/2)
    .heightIs(30);
    
    bankLabel = [[UILabel alloc] init];
    bankLabel.textColor = [UIColor blackColor];
    bankLabel.font = [UIFont systemFontOfSize:14];
    [cardView addSubview:bankLabel];
    bankLabel.sd_layout
    .leftSpaceToView(cardView, 80)
    .topSpaceToView(numberLabel, 15)
    .widthIs(kScreenWidth/2)
    .heightIs(30);
    
    for (NSInteger i = 0; i < 3; i++) {
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
        [cardView addSubview:line];
        line.sd_layout
        .topSpaceToView(cardView, 45+i*45)
        .leftSpaceToView(cardView, 0)
        .widthIs(kScreenWidth)
        .heightIs(1);
    }
    
    bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [_scrollView addSubview:bottomView];
    bottomView.sd_layout
    .leftSpaceToView(_scrollView, 0)
    .topSpaceToView(cardView, 30)
    .widthIs(kScreenWidth)
    .heightIs(85);
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    confirmBtn.backgroundColor = [UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmBtn setTitle:@"提现" forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    confirmBtn.layer.cornerRadius = 20;
    [confirmBtn addTarget:self action:@selector(confirmBtnHandler) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:confirmBtn];
    confirmBtn.sd_layout
    .leftSpaceToView(bottomView, 40)
    .topSpaceToView(bottomView, 5)
    .widthIs(kScreenWidth - 80)
    .heightIs(40);
    
    UILabel *bottomLabel = [[UILabel alloc] init];
    bottomLabel.textAlignment = NSTextAlignmentCenter;
    bottomLabel.text = @"提现后，一般七个工作日内到账";
    bottomLabel.font = [UIFont systemFontOfSize:12];
    bottomLabel.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
    [bottomView addSubview:bottomLabel];
    bottomLabel.sd_layout
    .leftSpaceToView(bottomView, 50)
    .topSpaceToView(confirmBtn, 5)
    .widthIs(kScreenWidth - 100)
    .heightIs(35);
    
    [_scrollView setupAutoContentSizeWithBottomView:bottomView bottomMargin:30];
}

-(void)confirmBtnHandler
{
    if ([priceField.text isEqualToString:@""]) {
        [self showMessage:@"请先输入金额"];
        return;
    }
    if ([dataSource[@"bankHolder"] isEqual:@""] || [dataSource[@"bankNumber"] isEqual:@""] || [dataSource[@"bankName"] isEqual:@""]) {
        [self showMessage:@"请补全银行账户信息"];
        return;
    }
    [[ToolNet sharedManager] checkCash:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] amount:priceField.text paymethod:0 cashType:type url:MCashCheck completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            priceField.text = nil;
            [self showMessage:responseJson[@"msg"]];
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }
    }];
}

-(void)createCoverView
{
    coverView = [[UIView alloc] init];
    coverView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:coverView];
    coverView.sd_layout
    .leftSpaceToView(_scrollView, 0)
    .topSpaceToView(whiteView, 0)
    .widthIs(kScreenWidth)
    .heightIs(110);
    coverView.hidden = YES;
    
    coverLabel = [[UILabel alloc] init];
    coverLabel.font = [UIFont systemFontOfSize:12];
//    coverLabel.text = @"实际提现¥0.0,";
    coverLabel.textColor = [UIColor blackColor];
    [coverView addSubview:coverLabel];
    coverLabel.sd_layout
    .leftSpaceToView(coverView, 20)
    .topSpaceToView(coverView, 0)
    .widthIs(110)
    .heightIs(35);
    NSMutableAttributedString *coverStr = [[NSMutableAttributedString alloc] initWithString:@"实际提现¥0.0,"];
    [coverStr addAttribute:NSForegroundColorAttributeName
                       value:[UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1]
                       range:NSMakeRange(5, coverStr.length-6)];
    coverLabel.attributedText = coverStr;
    [coverLabel setSingleLineAutoResizeWithMaxWidth:kScreenWidth/2-20];
    
    costLabel = [[UILabel alloc] init];
    costLabel.font = [UIFont systemFontOfSize:12];
    //    costLabel.text = @"扣除手续费500(手续费5%)";
    costLabel.textColor = [UIColor blackColor];
    [coverView addSubview:costLabel];
    costLabel.sd_layout
    .leftSpaceToView(declareLabel, 0)
    .topSpaceToView(coverView, 0)
    .widthIs(110)
    .heightIs(35);
    NSMutableAttributedString *costStr = [[NSMutableAttributedString alloc] initWithString:@"扣除手续费¥0，"];
    [costStr addAttribute:NSForegroundColorAttributeName
                      value:[UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1]
                      range:NSMakeRange(6, coverStr.length-7)];
    costLabel.attributedText = costStr;
    [costLabel setSingleLineAutoResizeWithMaxWidth:kScreenWidth/2-20];
    
    rebateLabel = [[UILabel alloc] init];
//    rebateLabel.text = @"返0积分";
    rebateLabel.font = [UIFont systemFontOfSize:12];
    rebateLabel.textColor = [UIColor blackColor];
    [coverView addSubview:rebateLabel];
    rebateLabel.sd_layout
    .leftSpaceToView(costLabel, 0)
    .topSpaceToView(coverView, 0)
    .widthIs(80)
    .heightIs(35);
    NSMutableAttributedString *rebateStr = [[NSMutableAttributedString alloc] initWithString:@"返0积分"];
    [rebateStr addAttribute:NSForegroundColorAttributeName
                    value:[UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1]
                    range:NSMakeRange(1, 1)];
    rebateLabel.attributedText = rebateStr;
    [rebateLabel setSingleLineAutoResizeWithMaxWidth:120];
    
    UILabel *serviceLabel = [[UILabel alloc] init];
    serviceLabel.text = @"(手续费20%返15%积分)";
    serviceLabel.font = [UIFont systemFontOfSize:11];
    serviceLabel.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
    [coverView addSubview:serviceLabel];
    serviceLabel.sd_layout
    .leftSpaceToView(coverView, 20)
    .topSpaceToView(costLabel, 0)
    .widthIs(150)
    .heightIs(20);
    
    UIView *bottomline = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    bottomline.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    [coverView addSubview:bottomline];
    bottomline.sd_layout
    .leftSpaceToView(coverView, 0)
    .topSpaceToView(serviceLabel, 5)
    .widthIs(kScreenWidth)
    .heightIs(1);
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 17, 32, 21)];
    imageView.image = [UIImage imageNamed:@"cm_bankcard"];
    [coverView addSubview:imageView];
    imageView.sd_layout
    .leftSpaceToView(coverView, 15)
    .topSpaceToView(bottomline, 12)
    .widthIs(32)
    .heightIs(21);
    
    UILabel *cardLabel = [[UILabel alloc] init];
    cardLabel.text = @"提现到银行卡";
    cardLabel.font = [UIFont systemFontOfSize:15];
    cardLabel.textColor = [UIColor blackColor];
    [coverView addSubview:cardLabel];
    cardLabel.sd_layout
    .leftSpaceToView(imageView, 15)
    .topSpaceToView(bottomline, 5)
    .widthIs(120)
    .heightIs(35);
}

-(void)hidekeyBoard
{
    [self.view endEditing:YES];
}

-(void)clickButton:(UIButton *)button
{
    for (UIButton *btn in headView.subviews) {
        if ([btn isKindOfClass:[UIButton class]]) {
            btn.selected = NO;
            btn.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
        }
    }
    button.selected = YES;
    button.backgroundColor = [UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1];
    
    if (button.tag == 100) {
        type = 0;
        declareView.hidden = NO;
        coverView.hidden = YES;
    }
    else {
        type = 1;
        declareView.hidden = YES;
        coverView.hidden = NO;
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidChange:(UITextField *)textField
{
    declareLabel.text = [NSString stringWithFormat:@"实际提现¥%.2f,",[textField.text floatValue]-[textField.text floatValue]*0.05];
    NSMutableAttributedString *declareStr = [[NSMutableAttributedString alloc] initWithString:declareLabel.text];
    [declareStr addAttribute:NSForegroundColorAttributeName
                       value:[UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1]
                       range:NSMakeRange(5, declareStr.length-6)];
    declareLabel.attributedText = declareStr;
        
    NSMutableAttributedString *chargeStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"扣除手续费¥%.2f",[textField.text floatValue]*0.05]];
    [chargeStr addAttribute:NSForegroundColorAttributeName
                      value:[UIColor blackColor]
                      range:NSMakeRange(0, 6)];
    chargeLabel.attributedText = chargeStr;

    coverLabel.text = [NSString stringWithFormat:@"实际提现¥%.2f,",[textField.text floatValue]-[textField.text floatValue]*0.2];
    NSMutableAttributedString *coverStr = [[NSMutableAttributedString alloc] initWithString:coverLabel.text];
    [coverStr addAttribute:NSForegroundColorAttributeName
                     value:[UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1]
                     range:NSMakeRange(5, coverStr.length-6)];
    coverLabel.attributedText = coverStr;
    
    NSMutableAttributedString *costStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"扣除手续费¥%.2f，",[textField.text floatValue]*0.2]];
    [costStr addAttribute:NSForegroundColorAttributeName
                    value:[UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1]
                    range:NSMakeRange(6, coverStr.length-6)];
    costLabel.attributedText = costStr;

    NSMutableAttributedString *rebateStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"返%.f积分",[textField.text floatValue]*0.15]];
    [rebateStr addAttribute:NSForegroundColorAttributeName
                      value:[UIColor colorWithRed:0.19 green:0.53 blue:0.98 alpha:1]
                      range:NSMakeRange(1, rebateStr.length-3)];
    rebateLabel.attributedText = rebateStr;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
